package com.example.shopspring.common;

import lombok.Data;

import java.util.Objects;

@Data
public class ResultData<T> {
    /**
     * 响应编码
     */
    private Integer code;
    /**
     * 响应信息
     */
    private String msg;
    /**
     * 响应数据
     */
    private T data;
    /**
     * 接口请求时间
     */
    private long timestamp;

    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }

    public ResultData(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ResultData(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static ResultData success() {
        return ResultData.success(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg());
    }

    public static ResultData success(int code, String msg) {
        ResultData<Object> resultData = new ResultData<>();
        resultData.setCode(code);
        resultData.setMsg(msg);
        return resultData;
    }

    public static ResultData success(Object data) {
        ResultData<Object> resultData = new ResultData<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg());
        if (Objects.nonNull(data)) {
            resultData.setData(data);
        }
        return resultData;
    }

    public static ResultData error(int code, String msg) {
        ResultData<Object> resultData = new ResultData<>(code, msg);
        return resultData;
    }

    public static ResultData error(ReturnCode returnCode) {
        ResultData<Object> resultData = new ResultData<>(returnCode.getCode(), returnCode.getMsg());
        return resultData;
    }
}